import xgboost as xgb
import scipy.stats as sps
import pandas as pd
import numpy as np
import warnings
import itertools
from sklearn.metrics import accuracy_score

warnings.filterwarnings(action='ignore')

path = '../data/'

train_basic = pd.read_pickle(path+'train_basic.pkl')
train_lda = pd.read_pickle(path+'train_lda_tfidf.pkl')
train_nmf = pd.read_pickle(path+'train_nmf_tfidf.pkl')
train_svd = pd.read_pickle(path+'train_svd_tfidf.pkl')
train_pos = pd.read_pickle(path+'train_pos.pkl')
train_w2v = pd.read_pickle(path+'train_w2v.pkl')

valid_basic = pd.read_pickle(path+'valid_basic.pkl')
valid_lda = pd.read_pickle(path+'valid_lda_tfidf.pkl')
valid_nmf = pd.read_pickle(path+'valid_nmf_tfidf.pkl')
valid_svd = pd.read_pickle(path+'valid_svd_tfidf.pkl')
valid_pos = pd.read_pickle(path+'valid_pos.pkl')
valid_w2v = pd.read_pickle(path+'valid_w2v.pkl')


y=pd.read_pickle(path+'train.pkl')['label']
y_valid = pd.read_pickle(path+'valid.pkl')['label']


X_train=np.hstack([train_basic,
                   train_lda,
                   train_nmf,
                   train_svd,
                   train_pos,
                   train_w2v])

X_valid = np.hstack([valid_basic,
                     valid_lda,
                     valid_nmf,
                     valid_svd,
                     valid_pos,
                     valid_w2v])

print(X_train.shape,X_valid.shape)

params={
    'max_depth':8,
    'nthread':18,
    'eta':0.03,
    'eval_metric':['error','logloss'],
    #'eval_metric':['logloss','error'],
    'objective':'binary:logistic',
    'subsample':0.7,
    'colsample_bytree':0.5,
    'silent':1,
    'seed':1123,
    'min_child_weight':10
    #'scale_pos_weight':0.5
}


dtrain=xgb.DMatrix(X_train,y)
dtest=xgb.DMatrix(X_valid,y_valid)

clf=xgb.train(params,dtrain,
              num_boost_round=1000,
              early_stopping_rounds=20,
              evals=[(dtrain,'Train'),(dtest,'Test')],
              verbose_eval=20)

# clf.save_model('../model/xgb_5_8.hdf5')



#ananlsis the result

valid_pred = clf.predict(dtest)
y_v = (valid_pred+0.5).astype(int)
acc =  accuracy_score(y_valid,y_v)

print('xgb model the accuracy on the valid set is : {}%'.format(round(acc* 100,2)))



#analysis the dev
test_basic = pd.read_pickle(path+'dev_basic.pkl')
test_lda = pd.read_pickle(path+'dev_lda_tfidf.pkl')
test_nmf = pd.read_pickle(path+'dev_nmf_tfidf.pkl')
test_svd = pd.read_pickle(path+'dev_svd_tfidf.pkl')
test_pos = pd.read_pickle(path+'dev_pos.pkl')
test_w2v = pd.read_pickle(path+'dev_w2v.pkl')


X_test = np.hstack([test_basic,
                    test_lda,
                    test_nmf,
                    test_svd,
                    test_pos,
                    test_w2v])

y_test = pd.read_pickle(path+'dev.pkl')['label']
dtest=xgb.DMatrix(X_test)

test_pred = clf.predict(dtest)
y_t = (test_pred+0.5).astype(int)
acc =  accuracy_score(y_test,y_t)

print('xgb model the accuracy on the dev set is : {}%'.format(round(acc* 100,2)))






